


/**
 * 统计val元素在 arr数组中出现的频次
 * @param  {[type]} arr [description]
 * @param  {[type]} val [description]
 * @return {[type]}     [description]
 */
function arrayFrequency(arr, val) {
	return arr.reduce((total, item) => {
		return total += val === item
	}, 0)
}

/**
 * 获取数组的最大值
 * @param  {[type]} arr [description]
 * @return {[type]}     [description]
 */
function arrayMax(arr) {
	return arr.reduce((max, item) => max > item ? max : item, arr[0]);
}


function maxPrice(arr) {
	return arr.reduce((maxPriceOne, item) =>{
		return  maxPriceOne.price > item.price ? maxPriceOne : item
	}, arr[0])
}

/**
 * 使用 reduce 实现数组去重
 * @param  {[type]} arr [description]
 * @return {[type]}     [description]
 */
function arrayUnique(arr) {
	return arr.reduce((pre, cur, index, arr) => {
		return !pre.includes(cur) ? [...pre, cur] : pre;
	}, [])
}




var  cart = [
  { name: "iphone", price: 12000 },
  { name: "imac", price: 25000 },
  { name: "ipad", price: 3600 }
];

/**
 * 获取价格超过1万元商品的名称
 * @param  {[type]} goods [description]
 * @param  {[type]} price [description]
 * @return {[type]}       [description]
 */
function getNameByPrice(goods, price) {
  return goods
    .reduce(function(arr, cur) {
      if (cur.price > price) arr.push(cur);
      return arr;
    }, [])
    .map(function(item) {
      return item.name;
    });
}

function getNameByPriceV2(goods, price){
	return goods.reduce((arr, cur) => {
		cur.price > price && arr.push(cur.name)
		return arr
	}, []);
}



var  cart = [
  { name: "imac", price: 25000 },
  { name: "iphone", price: 12000 },
  { name: "imac", price: 25000 },
  { name: "ipad", price: 3600 },
  { name: "ipad", price: 3600 }
];

/**
 *利用 reduce 去重
 * @param  {[type]} goods [description]
 * @return {[type]}       [description]
 */
function filterGoods(goods) {
	return goods.reduce((arr, cur, index) => {
		var  find = arr.find(item => {
			// 名称相同 就认为两个商品相同
			return item.name === cur.name
		})

		!find && arr.push(cur);
		return arr 
	}, [])
}


function filterGoodsV2(goods) {
	var ret = goods.reduce((arr, cur, index) => {
		// 第一次时候 采用个临时属性变量， 已经去重的商品名称的列表， 做判断
		index === 0 && (arr.names = [])
		
		if(!arr.names.includes(cur.name)) {
			arr.push(cur)
			arr.names.push(cur.name)
		}
		return arr
	}, [])

	delete ret.names 
	return ret 
}



